public class ThreadDemoVolatile {
    static class Counter{
        //变量
        private volatile int number=0;

        //循环次数
        private int MAX_COUNT=0;

        public Counter(int MAX_COUNT) {
            this.MAX_COUNT = MAX_COUNT;
        }

        //++方法
        public void incr(){
            for (int i = 0; i <MAX_COUNT ; i++) {
                number++;
            }
        }
        //--方法
        public void decr(){
            for (int i = 0; i < MAX_COUNT; i++) {
                number--;
            }
        }

        public int getNumber(){
            return number;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Counter counter=new Counter(100000);
        Thread  t1=new  Thread(()->{
            counter.incr();
        });
        t1.start();

        Thread t2=new Thread(()->{
           counter.decr();
        });
        t2.start();

        //等待线程执行完成
        t1.join();
        t2.join();
        System.out.println("最终的结果是:"+counter.getNumber());
    }
}
